const students = [
    {
        name: 'zhangsan',
        score: 140,
    },
    {
        name: 'lisi',
        score: 120,
    },
    {
        name: 'wangwu',
        score: 90,
    },
    {
        name: 'liuliu',
        score: 40,
    },
    {
        name: 'zhaoqi',
        score: 130,
    },
];

const goodStudents = [
    'zhangsan',
    'lisi',
    'zhaoqi',
];

// 求优等生平均分
console.log(
    // N
    students
        // O(M*N)
        .filter((student) => {
            // M
            return goodStudents.includes(student.name);
        })
        .reduce((prevResult, current) => {
            return prevResult + current.score;
        }, 0) / goodStudents.length
);

// 一次循环
function getAvg () {
    return students.reduce((prevResult, current) => {
        const isGoodStudent = goodStudents.includes(current.name);
        return prevResult + (isGoodStudent ? current.score : 0);
    }, 0) / goodStudents.length;
}

console.log(getAvg());

